#!/usr/bin/env python
# -*- coding:utf-8 -*-

"""
@version: ??
@author: happiness
@license: Apache Licence 
@contact: happiness_ws@163.com
@software: PyCharm
@file: mnist.py
@time: 2017/11/14 8:49
"""
import numpy as np
import struct
import matplotlib.pyplot as plt
import os

filename = 'train-images.idx3-ubyte'
file_path = os.path.join('.',filename)
binfile = open(file_path, 'rb')
buf = binfile.read()

index = 0
# 以大端方式读取前四个字节
magic, numImages, numRows, numColumns = struct.unpack_from('>IIII', buf, index)
#计算格式字符串所对应的结果的长度
index += struct.calcsize('>IIII')

im = struct.unpack_from('>784B', buf, index)
index += struct.calcsize('>784B')

im = np.array(im)
im = im.reshape(28, 28)

fig = plt.figure()
plotwindow = fig.add_subplot(111)
plt.imshow(im, cmap='gray')
plt.show()

if __name__ == '__main__':
    pass
